package pATT.profile;

import java.util.Vector;

import pATT.core.FastVector;
import pATT.logger.Message;

public class ComponentNode extends ComponentTree {

	/**
	 * 
	 */
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */

	private Vector<ComponentTree> sons = new Vector<ComponentTree>();
	private FastVector ramas;
	
	public ComponentNode(String nameAttribute, Vector<ComponentTree> sons,FastVector ramas) {
		super(nameAttribute);
		this.sons = sons;
		this.ramas = ramas;
	}
	
	@Override
	public ComponentTree appliesTo(Vector situation) {
		int pos = -1;
		for (int i = 0; i < ramas.size(); i++) {
			String rama = (String)ramas.elementAt(i);
			if(getValueAtt(situation).equals(rama)){
				pos = i;
				break;
			}
		}
		if(pos != -1){
			return sons.elementAt(pos).appliesTo(situation);
		}
		return null;

	}

	public void addComponent(ComponentTree son){
		sons.addElement(son);
	}

	public Vector<ComponentTree> getSons() {
		return sons;
	}

	public FastVector getRamas() {
		return ramas;
	}

	@Override
	public StringBuffer toText() {
		String text = new String();
		text += " [Component="+"'Node' name='"+nameAtt+"'"+" ramas"+getStringRamas()+
				"\n elements{ ";
		for (int i = 0; i < sons.size(); i++) {
			ComponentTree son = sons.get(i);
			if(i==0){
				text += son.toText()+"\n";
			}else{
				text += Message.SPACE+son.toText()+"\n";
			}
		}	
				
		text += Message.SPACE+"} ]";

		return new StringBuffer(text);
	}

	private String getStringRamas() {
		String text = "{ ";
		for (int i = 0; i < ramas.size(); i++) {
			String rama = (String) ramas.elementAt(i);
			text += rama+" ";
			
		}
//		while (ramas.hasMoreElements()) {
//			String rama = (String) ramas.nextElement();
//			System.out.println("rama: "+rama);
//			text += rama+" ";
//			
//		}
		text += "}";
		return text;
	}
	
}
